<?php //pack11class

class Login extends Module {

    public function setup() {

        $form = new Form;
        $form->action="?admin";

        $username=new TextInput();
        $username->property_name="user";
        $username->id="user";
        $username->label="Username: ";
        $form->addChild($username);

        $password=new PasswordInput();
        $password->property_name="pass";
        $password->id="pass";
        $password->label="Password: ";
        $form->addChild($password);

        $captcha = new SimpleModule();
        $captcha->addStyle("width","100%");
        $captcha->addStyle("background","#FFFFFF");
        $cid=Captcha::setHash();
        $captcha->setBody(str_replace('\"','"',"<div id=\"captcha\"><img src=\"?response=CaptchaGenerator&cid=".$cid."\"/><input type=\"hidden\" name=\"cid\" value=\"".$cid."\"/></div>"));
        $form->addChild($captcha);

        $capt=new TextInput();
        $capt->property_name="c_value";
        $capt->id="c_value";
        $capt->label="Sequrity code: ";
        $form->addChild($capt);

        $submit=new FormButton;
        $submit->value="Log in";
        $submit->type=FormButton::TYPE_SUBMIT;
        $submit->setID("LoginSubmit");
        $submit->setClass("LoginSubmit");
        $form->addButton($submit);

        $this->addChild($form);

    }

    public static function getUser($login,$pass) {
        $captchaTest=MySQLGet::process("Select `number` from ".MySQL::TABLE_CAPTCHA." where MD5(`hash`)='".$_POST['cid']."' limit 1;");
        $captcha=$captchaTest[0]['number'];
        if($captcha!=$_POST['c_value']) {
            /*echo "Captcha: ".$captcha." ".MySQLGet::getQuery()."<br/>";
            print_r($_POST);*/
            return false;
        }
        $res=MySQLGet::process("Select `id`, `cookie` from ".MySQL::TABLE_USERS." where `name`='{$login}' and `pass`='".MD5($pass)."' limit 1;");
        trace(MySQLGet::getQuery());
        if($res) {
            setcookie("dekaro_master", $res[0]["cookie"], time()+3600*24*30);
            setcookie("userin", 1);
            $_SESSION["uid"]=MD5(rand(0, 999999));
            $setuid="Update ".MySQL::TABLE_USERS." set `uid`='".$_SESSION["uid"]."' where `id`='".$res[0]["id"]."';";
            trace($setuid);
            mysql_query($setuid);
        }
        return ($res)? true:false;
    }

    

}
?>
